#Boa:Dialog:Dialog1

import wx
import wx.grid
import consultasBD
import utiles
import sincronizar_tabla  
import os

ROJO = (255, 145, 145, 255)
VERDE = (145, 255, 145, 255)
AZUL = (145, 145, 255, 255)
BLANCO = (255, 255, 255, 255)
AMARILLO = (254, 248, 191, 255)
GRIS = ( 220, 220, 220, 255)
NEGRO = ( 0, 0, 0, 255)
VERDENEGRO = ( 47, 79, 79, 255)

def create(parent):
    return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1BUTTON1, wxID_DIALOG1BUTTON2, wxID_DIALOG1BUTTON3, 
 wxID_DIALOG1BUTTON4, wxID_DIALOG1COMBOBOX1, wxID_DIALOG1COMBOBOX2, 
 wxID_DIALOG1GRID1, wxID_DIALOG1RADIOBUTTON1, wxID_DIALOG1RADIOBUTTON2, 
 wxID_DIALOG1STATICTEXT1, wxID_DIALOG1STATICTEXT2, wxID_DIALOG1STATICTEXT3, 
 wxID_DIALOG1STATICTEXT4, wxID_DIALOG1STATICTEXT5, wxID_DIALOG1STATICTEXT6, 
 wxID_DIALOG1STATICTEXT7, wxID_DIALOG1STATICTEXT8, wxID_DIALOG1TEXTCTRL3, 
 wxID_DIALOG1TEXTCTRL4, 
] = [wx.NewId() for _init_ctrls in range(20)]

class Dialog1(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(44, 79), size=wx.Size(746, 428),
              style=wx.DEFAULT_DIALOG_STYLE, title='Eliminar Transferencia')
        self.SetClientSize(wx.Size(738, 401))

        self.staticText1 = wx.StaticText(id=wxID_DIALOG1STATICTEXT1,
              label='Transferencias', name='staticText1', parent=self,
              pos=wx.Point(16, 2), size=wx.Size(71, 13), style=0)

        self.comboBox1 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX1,
              name='comboBox1', parent=self, pos=wx.Point(16, 32),
              size=wx.Size(72, 21), style=0, value='')
        self.comboBox1.SetLabel('')

        self.staticText3 = wx.StaticText(id=wxID_DIALOG1STATICTEXT3,
              label='Sucursal Origen', name='staticText3', parent=self,
              pos=wx.Point(96, 16), size=wx.Size(75, 13), style=0)

        self.textCtrl3 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL3, name='textCtrl3',
              parent=self, pos=wx.Point(96, 32), size=wx.Size(24, 21), style=0,
              value='')
        self.textCtrl3.SetEditable(False)

        self.comboBox2 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX2,
              name='comboBox2', parent=self, pos=wx.Point(120, 32),
              size=wx.Size(152, 21), style=0, value='')
        self.comboBox2.SetLabel('')
        self.comboBox2.Bind(wx.EVT_COMBOBOX, self.OnCambiarSucursal,
              id=wxID_DIALOG1COMBOBOX2)

        self.staticText4 = wx.StaticText(id=wxID_DIALOG1STATICTEXT4,
              label='P.T.C.', name='staticText4', parent=self, pos=wx.Point(280,
              16), size=wx.Size(31, 13), style=0)

        self.textCtrl4 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL4, name='textCtrl4',
              parent=self, pos=wx.Point(280, 32), size=wx.Size(64, 21), style=0,
              value='')

        self.button1 = wx.Button(id=wxID_DIALOG1BUTTON1, label='Buscar',
              name='button1', parent=self, pos=wx.Point(352, 32),
              size=wx.Size(75, 23), style=0)

        self.radioButton1 = wx.RadioButton(id=wxID_DIALOG1RADIOBUTTON1,
              label='Eliminar solo producto', name='radioButton1', parent=self,
              pos=wx.Point(448, 26), size=wx.Size(136, 13), style=0)
        self.radioButton1.SetValue(True)

        self.radioButton2 = wx.RadioButton(id=wxID_DIALOG1RADIOBUTTON2,
              label='Eliminar Transferencia completa', name='radioButton2',
              parent=self, pos=wx.Point(448, 42), size=wx.Size(176, 13),
              style=0)
        self.radioButton2.SetValue(False)

        self.button2 = wx.Button(id=wxID_DIALOG1BUTTON2, label='Limpiar',
              name='button2', parent=self, pos=wx.Point(655, 32),
              size=wx.Size(75, 23), style=0)
        self.button2.Bind(wx.EVT_BUTTON, self.OnLimpiar, id=wxID_DIALOG1BUTTON2)

        self.button3 = wx.Button(id=wxID_DIALOG1BUTTON3, label='Eliminar',
              name='button3', parent=self, pos=wx.Point(360, 360),
              size=wx.Size(75, 23), style=0)

        self.staticText2 = wx.StaticText(id=wxID_DIALOG1STATICTEXT2,
              label='Pendientes', name='staticText2', parent=self,
              pos=wx.Point(16, 16), size=wx.Size(53, 13), style=0)

        self.staticText5 = wx.StaticText(id=wxID_DIALOG1STATICTEXT5,
              label='DOBLE CLICK IZQUIERDO', name='staticText5', parent=self,
              pos=wx.Point(16, 360), size=wx.Size(136, 13), style=0)
        self.staticText5.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              'Tahoma'))

        self.staticText6 = wx.StaticText(id=wxID_DIALOG1STATICTEXT6,
              label='DOBLE CLICK DERECHO', name='staticText6', parent=self,
              pos=wx.Point(16, 376), size=wx.Size(124, 13), style=0)
        self.staticText6.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              'Tahoma'))

        self.staticText7 = wx.StaticText(id=wxID_DIALOG1STATICTEXT7,
              label='- marca para borrar', name='staticText7', parent=self,
              pos=wx.Point(160, 360), size=wx.Size(94, 13), style=0)

        self.staticText8 = wx.StaticText(id=wxID_DIALOG1STATICTEXT8,
              label='- quita marca de borrado', name='staticText8', parent=self,
              pos=wx.Point(160, 376), size=wx.Size(119, 13), style=0)

        self.grid1 = wx.grid.Grid(id=wxID_DIALOG1GRID1, name='grid1',
              parent=self, pos=wx.Point(16, 64), size=wx.Size(712, 288),
              style=0)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_RIGHT_DCLICK,
              self.OnDobleClickDerecho)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK,
              self.OnDobleClickIzquierdo)

        self.button4 = wx.Button(id=wxID_DIALOG1BUTTON4, label='Salir',
              name='button4', parent=self, pos=wx.Point(655, 360),
              size=wx.Size(75, 23), style=0)
        self.button4.Bind(wx.EVT_BUTTON, self.OnSalir, id=wxID_DIALOG1BUTTON4)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.sucursalDic = {}
        self.sucursal = utiles.datosSucursal()[0]
        self.nombresucursal = utiles.datosSucursal()[1]
        self.cargarSucursales()
        self.cargarTransferenciasPendientes()
        self.crearGrillas()

    def OnSalir(self, event):
        self.Close()

    def OnLimpiar(self, event):
        self.comboBox1.SetSelection(-1)
        self.textCtrl3.SetLabel("")
        self.comboBox2.SetSelection(-1)
        self.textCtrl4.SetLabel("")
        self.radioButton1.SetValue(True)
        self.grid1.ClearGrid()
        for fil in xrange(self.grid1.GetNumberRows()):
            self.grid1.DeleteRows()
        self.grid1.AppendRows()
        self.comboBox1.SetFocus()

    def OnDobleClickDerecho(self, event):
        fil = self.grid1.GetGridCursorRow()
        col = self.grid1.GetGridCursorCol()

    def OnDobleClickIzquierdo(self, event):
        fil = self.grid1.GetGridCursorRow()
        col = self.grid1.GetGridCursorCol()

    def buscarDatosProductos(self, ptc):
        sucursal = utiles.getSucursalActiva()
        r01 = "SELECT p1.ptc,p1.codsussan,p2.descri as descripcion,\n "
        r02 = "       p1.talle,p1.color,p3.nombre as colornombre,\n "
        r03 = "       p2.preciolt1 as precio,p5.nombre,p4.stock \n "
        r04 = "FROM \n "
        r05 = "  (SELECT id as ptc,codsussan,talle,color \n "
        r06 = "   FROM producto_talle_color \n "
        r07 = "   WHERE id='"+ptc+"') AS p1,\n "
        r08 = "  productos AS p2, \n "
        r09 = "  colores AS p3, \n "
        r10 = "  stock AS p4, \n "
        r11 = "  proveedor AS p5 \n "
        r12 = "WHERE \n "
        r13 = "  p1.codsussan = p2.codsussan AND \n "
        r14 = "  p1.color = p3.idcolor AND \n "
        r15 = "  p1.ptc = p4.ptc AND \n "
        r16 = "  p4.sucursal = '"+sucursal+"' AND \n "
        r17 = "  p2.proveedor = p5.idproveedor \n "
        sql = r01+r02+r03+r04+r05+r06+r07+r08+r09+r10+r11+r12+r13+r14+r15+r16+r17
        resultado = consultasBD.ordenCrudaDict(sql)
        if resultado == None or resultado == ():
            print "Producto no encontrado en esta sucursal"
            return False,()
        return True,resultado[0]
    
    def ajustarAnchoColumnas(self):
        for y in xrange(self.grid1.GetNumberCols()):
            self.grid1.AutoSizeColumn(y,True)

    def crearGrillas(self):
        #PTC,CodSussan,Cant,Descripcion,Talle,Color,Proveedor
        #0   1         2    3           4     5     6      
        columnas = ['P.T.C','Codigo\nSussan','Cant','Descripcion','Talle','Color','Proveedor']
        self.grid1.CreateGrid(1,len(columnas))
        for i, col in enumerate(columnas): 
            self.grid1.SetColLabelValue(i, col)
        self.grid1.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)
        self.grid1.EnableEditing(False)
        self.ajustarAnchoColumnas()
        self.grid1.Refresh()

    def cargarSucursales(self):
        sql = "SELECT codsucursal,nombre FROM sucursales WHERE activa = 0"
        resultados = consultasBD.ordenCruda(sql)
        for sucursal in resultados:
            self.comboBox2.Append(sucursal[1])
            self.sucursalDic[sucursal[1]] = str(sucursal[0])
        self.comboBox2.SetEditable(False)

    def cargarTransferenciasPendientes(self):
        r1 = "SELECT idtransferencia FROM transferencia "
        r2 = "WHERE estado='0' AND idsucdestino='1' "
        r3 = "GROUP BY idtransferencia ORDER BY idtransferencia "
        sql = r1+r2+r3
        resultados = consultasBD.ordenCruda(sql)
        for item in resultados:
            self.comboBox1.Append(item[0])

    def OnCambiarSucursal(self, event):
        sucursal = self.comboBox2.GetLabel()
        indice = self.sucursalDic[sucursal]
        self.textCtrl3.SetLabel(str(indice))
